﻿@page "/AgentGroup/{Id:guid?}"
@page "/AgentGroup/{Action}/{Id:guid?}"
@using Gurux.DLMS.AMI.Client.Pages.Device
@using Gurux.DLMS.AMI.Client.Pages.User
@using Gurux.DLMS.AMI.Shared.DIs
@using Gurux.DLMS.AMI.Shared.DTOs.Agent
@using Gurux.DLMS.AMI.Shared.Rest;
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication
@using Gurux.DLMS.AMI.Shared.DTOs.Authentication
@using Gurux.DLMS.AMI.Shared
@using Gurux.DLMS.AMI.Shared.Enums
@using Gurux.DLMS.AMI.Shared.DTOs;

@attribute [Authorize(Roles = GXRoles.Admin + "," + GXRoles.AgentManager + "," + GXRoles.AgentGroupManager)]
@inject NavigationManager NavigationManager
@inject HttpClient Http
@inject IGXNotifier Notifier

<h1>
    @Title
</h1>
<CascadingValue Value="this">
    <TabControl>
        <TabTextTemplate>
            <span class=@context.Icon aria-hidden="true"></span>
            @context.Text
        </TabTextTemplate>
        <ChildContent>
            <AuthorizeView Roles=@GXRoles.ToString(GXRoles.Admin, GXRoles.AgentGroupManager)>
                <TabPage Text=@Properties.Resources.GeneralInformation Icon="oi oi-timer">
                    <div style="position: relative; width: 100%; ">
                        <AgentGroup Action="@Action"></AgentGroup>
                    </div>
                </TabPage>
            </AuthorizeView>
            <AuthorizeView Roles=@GXRoles.ToString(GXRoles.Admin, GXRoles.UserGroupManager)>
                <TabPage Text=@Properties.Resources.Agents Icon="oi oi-timer">
                    <div style="position: relative; width: 100%; ">
                        <AgentsSelector></AgentsSelector>
                    </div>
                </TabPage>
            </AuthorizeView>
            <AuthorizeView Roles=@GXRoles.ToString(GXRoles.Admin, GXRoles.UserGroupManager)>
                <TabPage Text=@Properties.Resources.UserGroupsTitle Icon="oi oi-timer">
                    <div style="position: relative; width: 100%; ">
                        <UserGroupsSelector></UserGroupsSelector>
                    </div>
                </TabPage>
            </AuthorizeView>
            <AuthorizeView Roles=@GXRoles.ToString(GXRoles.Admin, GXRoles.UserGroupManager)>
                <TabPage Text=@Properties.Resources.DeviceGroups Icon="oi oi-timer">
                    <div style="position: relative; width: 100%; ">
                        <DeviceGroupsSelector></DeviceGroupsSelector>
                    </div>
                </TabPage>
            </AuthorizeView>
        </ChildContent>
    </TabControl>
</CascadingValue>
<br />
@code {
    /// <summary>
    /// Action.
    /// </summary>
    [Parameter]
    public string? Action { get; set; }

    /// <summary>
    /// Active page.
    /// </summary>
    [Parameter]
    public string? Page { get; set; }

    /// <summary>
    /// Target ID.
    /// </summary>
    [Parameter]
    public Guid? Id { get; set; }

    /// <summary>
    /// Page title.
    /// </summary>
    private string? Title;

    /// <summary>
    /// Active item.
    /// </summary>
    public GXAgentGroup Active = new GXAgentGroup(null);

    protected override async Task OnInitializedAsync()
    {
        try
        {
            switch (Action)
            {
                case "Add":
                    Title = Properties.Resources.Add;
                    break;
                case "Edit":
                    Title = Properties.Resources.Edit;
                    break;
                case "Remove":
                    Title = Properties.Resources.Remove;
                    break;
                default:
                    ClientHelpers.NavigateToLastPage(NavigationManager, Notifier);
                    return;
            }
            Title += " " + Properties.Resources.AgentGroup;
            if (Notifier == null)
            {
                throw new ArgumentException(Properties.Resources.InvalidNotifier);
            }
            Notifier.ProgressStart();
            Notifier.ClearStatus();
            if (string.Compare(Action, "Add", true) != 0)
            {
                //Get agent group data.
                var tmp = (await Http.GetAsJsonAsync<GetAgentGroupResponse>(string.Format("api/AgentGroup?Id={0}", Id)));
                if (tmp?.Item != null)
                {
                    Active = tmp.Item;
                    StateHasChanged();
                }
                else
                {
                    NavigationManager.NavigateTo("404");
                }
            }
        }
        catch (AccessTokenNotAvailableException exception)
        {
            exception.Redirect();
        }
        catch (Exception ex)
        {
            Notifier?.ProcessError(ex);
        }
        finally
        {
            Notifier?.ProgressEnd();
        }
    }
}

